IF EXISTS (SELECT *  FROM sys.objects  WHERE object_id = OBJECT_ID(N'fn_VendorXmissionInspection_GetTopTypeString') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	DROP FUNCTION fn_VendorXmissionInspection_GetTopTypeString

GO

CREATE FUNCTION [dbo].[fn_VendorXmissionInspection_GetTopTypeString]
(
	  @XMissionPoleId int
	 ,@IsAsset bit
)
RETURNS varchar(MAX)
AS
BEGIN

	DECLARE @Join varchar(MAX)
	SET @Join = ''
	
	IF (@IsAsset = 1)
	BEGIN
		SELECT 
			@Join = @Join + TopType.[Name] + ' - '
		FROM 
			tb_Utility_Asset_XMissionStructures_Poles XPole
		INNER JOIN tb_XMissionStructures_TopTypes TopType
			ON TopType.XMissionStructures_topTypeId = XPole.XMissionStructures_PoleTopTypeFk
		WHERE XPole.Utility_Asset_XMissionStructures_PoleId = @XMissionPoleId
	END	
	ELSE
	BEGIN
		SELECT 
			@Join = @Join + TopType.[Name] + ' - '
		FROM 
			tb_Vendor_InspVisit_XmissionStructures_Poles XPole
		INNER JOIN tb_XMissionStructures_TopTypes TopType
			ON TopType.XMissionStructures_topTypeId = XPole.TopTypeFk
		WHERE XPole.vendor_inspvisit_XMissionStructure_PoleId = @XMissionPoleId
	END

	IF (@Join <> '' AND NOT @Join IS NULL)
		SET @Join = LEFT(@Join, LEN(@Join) - 2) 

	RETURN @Join

END

